import moment from 'moment'
import {regLevels, jobNatures} from '../global'

moment.locale('zh-cn')


export default  {

  install:function(Vue){
    Vue.filter('formatNumber' ,this.formatNumber )
  },

  getQueryString : function (name){
    var r = window.location.search.substr(1).match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"));
    if(r) {
      return r[2];
    }
    return null;
  },
  formatTime : function (time , format ) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD H:mm:ss')
  },
  formatDate:function (time , format ) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD')
  },
  firstDayOfMonth:function(){
    return moment().startOf('month');
  },
  endDayOfMonth:function(){
    return moment().endOf('month')
  },

  getRegLevelVal: function(level) {
    let label = ''
    regLevels.forEach((val) => {
      if (val.value == level) {
        label = val.label
        return false
      }
    })
    return label
  },

  getJobNature: function(jobNature) {
    let label = ''
    jobNatures.forEach((val) => {
      if (val.value == jobNature) {
        label = val.label
        return false
      }
    })
    return label
  },

  formatNumber:function(val , fixed){
    if (val === Infinity || isNaN(parseFloat(val))) {
      return 0.00
    } else {
      return parseFloat(val).toFixed(fixed)
    }
  },
  initReEdit:function(){
    setTimeout(function(){
      //重新编辑公式
      $($(document).find('iframe').contents().find('body.view')).on('click','.kfformula',function(){
        let viewArr=$(document).find('iframe').contents().find('body.view');
        for(var i=0;i<viewArr.length;i++){
          if(viewArr[i]===$(this).closest('body.view')[0]){
            $($('.edui-for-插入kityformula')[i]).find('.edui-default').click();
          }
        }
      });
      //重新编辑latex
      $($(document).find('iframe').contents().find('body.view')).on('click','.latex',function(){
        let viewArr=$(document).find('iframe').contents().find('body.view');
        for(var i=0;i<viewArr.length;i++){
          if(viewArr[i]===$(this).closest('body.view')[0]){
            $($('.edui-for-解析latex')[i]).find('.edui-default').click();
          }
        }
      });

    },1000)
  },
  searchHashParam: function(query){
    let paramObj = this.getHashParamObj();
    query = Object.assign({},query,paramObj);
    return query;
  },
  formatCommaStyle: function(str){
    if(!/^(\-|\+)?\d+(\.\d+)?$/.test(str)) return;
    let values = '';
    if(str.indexOf(".") >= 0){
      values = str.split(".")[1];
      str = str.split(".")[0];
    }
    var re=/(?=(?!(\b))(\d{3})+$)/g;
    str = str.replace(re,",");
    if(values != ''){
      str = str + "." + values;
    }
    return str;
  },
  getHashParamObj: function(){
    let hash = window.location.hash;

    let search = hash ? hash.split("?")[1] : null;
    let paramArr = search ? search.split("&") : [];
    let paramObj = {};
    for(let i = 0 ; i < paramArr.length ; i ++){
      let partOfParam = paramArr[i].split("=");
      let paramVal = decodeURI(partOfParam[1]);
      if(paramVal.indexOf("[") >= 0){
        if(paramVal == "[]"){
          paramObj[partOfParam[0]] = [];
        }else {
          paramObj[partOfParam[0]] = paramVal.replace("[","").replace("]","").split(",");
        }
      }else{
        paramObj[partOfParam[0]] = paramVal;
      }
    }
    return paramObj;
  },
  getHashParamVal: function(name){
    let paramObj = this.getHashParamObj();
    return paramObj[name];
  },
  getUuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
  },
  isEmptyObj(obj){
    for(let i in obj){
      return false
    }
    return true
  }
}
